<?php

/**
 * 漫道短信类，必须 SMS::getInstance来获取实例
 * @author lovebing(lovebing365@126.com) created on 2014-4-8
 *
 */
class SMSMD extends SMSBase implements SMSInterface {

    protected $apiUrl = 'http://sdk2.entinfo.cn:8060/webservice.asmx/mt';
    private static $instances = null;

    /**
     * 获取实例id
     * @param string $host
     * @param int $port
     * @param string $user
     * @param string $pwd
     * @return string
     */
    public static function getInstanceId($user = '', $pwd = '') {
        return md5($user . $pwd);
    }
    
    /**
     * 获取实例
     * @param string $user
     * @param string $pwd
     * @param string $sign
     * @return Email
     */
    public static function getInstance($user = '', $pwd = '', $sign = null) {
        $id = self::getInstanceId($user, $pwd);
        if (!isset(self::$instances[$id]) || !is_a(self::$instances[$id], 'SMS')) {
            self::$instances[$id] = new self($user, $pwd, $sign);
        }
        $sms = self::$instances[$id];
        return $sms;
    }

    /**
     * 解析短信发送结果xml
     * @param string $xml
     * @return int
     */
    private function parseResult($xml) {
        if (empty($xml)) {
            echo '3333';
            exit;
            return -100;
        }
        $dom = new DOMDocument ();
        $dom->loadXML($xml);
        $res = $dom->getElementsByTagName('string');
        if ($res->length == 0) {
            return - 100;
        }
        $code = trim($res->item(0)->textContent);
        $code = is_numeric($code) ? floatval($code) : - 101;
        return $code;
    }

    /**
     * 发送
     * @param string $mobile
     * @param string $msg
     * @return int
     * @throws Exception
     */
    public function send($mobile, $msg) {
        $params = array(
            'sn' => $this->user,
            'pwd' => strtoupper(md5($this->user . $this->pwd)),
            'mobile' => $mobile, // 手机号 多个用英文的逗号隔开 post理论没有长度限制.推荐群发一次小于等于10000个手机号
            'content' => iconv('utf-8', 'gb18030', $msg . $this->sign), // 短信内容
            'ext' => '',
            'stime' => '', // 定时时间 格式为2011-6-29 11:09:21
            'rrid' => ''
        );

        $http = HTTPClient::getInstance();
        $res = $http->post($this->apiUrl, $params);
        $errorCode = $http->getErrorCode();
        if (!empty($errorCode)) {
            throw new Exception($http->getErrorMessage(), $errorCode);
        }
        $code = $this->parseResult($res);

        if($code < 0) {
            throw new Exception($this->getErrorMessage($code), $code);
        }
        return $code;
    }

    /**
     * 根据code获取错误信息
     * @param int $errorCode
     * @return string
     */
    public function getErrorMessage($errorCode) {
        if($errorCode >=0) {
            return '';
        }
        $msg = "未知错误{$errorCode}";
        switch ($errorCode) {
            case -2:
                $msg = '帐号/密码不正确';
                break;
            case -4:
                $msg = '余额不足支持本次发送';
                break;
            case -5:
                $msg = '数据格式错误';
                break;
            case -6:
                $msg = '参数有误';
                break;
            case -7:
                $msg = '权限受限';
                break;
            case -8:
                $msg = '流量控制错误';
                break;
            case -9:
                $msg = '扩展码权限错误';
                break;
            case -10:
                $msg = '内容长度长';
                break;
            case -11:
                $msg = '内部数据库错误';
                break;
            case -12:
                $msg = '序列号状态错误';
                break;
            case -14:
                $msg = '服务器写文件失败';
                break;
            case -17:
                $msg = '没有权限';
                break;
            case -19:
                $msg = '禁止同时使用多个接口地址';
                break;
            case -20:
                $msg = '相同手机号，相同内容重复提交';
                break;
            case -22:
                $msg = 'Ip鉴权失败';
                break;
            case -23:
                $msg = '缓存无此序列号信息';
                break;
            case -601:
                $msg = '序列号为空，参数错误';
                break;
             case -602:
                $msg = '序列号格式错误，参数错误';
                break;            
             case -603:
                $msg = '密码为空，参数错误';
                break;
            case -604:
                $msg = '手机号码为空，参数错误';
                break;
             case -605:
                $msg = '内容为空，参数错误';
                break;
            case -606:
                $msg = 'ext长度大于9，参数错误';
                break;
            case -607:
                $msg = '参数错误 扩展码非数字 ';
                break;
              case -608:
                $msg = '参数错误 定时时间非日期格式 ';
                break;
            case -609:
                $msg = 'rrid长度大于18,参数错误 ';
                break;
            case -610:
                $msg = '参数错误 rrid非数字';
                break;
            case -611:
                $msg = '参数错误 内容编码不符合规范';
                break;  
        }
        return $msg;
    }

}
